﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using ImagingSDK.Resources;
using Microsoft.Phone.Tasks;
using System.Windows.Media.Imaging;
using Nokia.Graphics.Imaging;
using Windows.Storage.Streams;
using Microsoft.Xna.Framework.Media;
using System.Runtime.InteropServices.WindowsRuntime;

namespace ImagingSDK
{
    public partial class MainPage : PhoneApplicationPage
    {
        WriteableBitmap originalBitmap;
        WriteableBitmap filteredBitmap;
        FilterEffect myEffect = null;
        // Constructor
        public MainPage()
        {
            InitializeComponent();

            originalBitmap = new WriteableBitmap((int)originalImage.Width, (int)originalImage.Height);
            filteredBitmap = new WriteableBitmap((int)filteredImage.Width, (int)filteredImage.Height);
            // Sample code to localize the ApplicationBar
            //BuildLocalizedApplicationBar();
        }

        private void pickerBtn_Click(object sender, RoutedEventArgs e)
        {
            PhotoChooserTask picker = new PhotoChooserTask();
            picker.Completed += picker_Completed;
            picker.Show();
        }

        async void picker_Completed(object sender, PhotoResult e)
        {
            originalBitmap.SetSource(e.ChosenPhoto);
            originalImage.Source = originalBitmap;

            e.ChosenPhoto.Position = 0;

            var imageStream = new StreamImageSource(e.ChosenPhoto);
            myEffect = new FilterEffect(imageStream);

            var myFilter = new CartoonFilter();
            myEffect.Filters = new[] { myFilter };

            var renderer = new WriteableBitmapRenderer(myEffect, filteredBitmap);
            filteredBitmap = await renderer.RenderAsync();
            filteredImage.Source = filteredBitmap;
        }

        private async void saveBtn_Click(object sender, RoutedEventArgs e)
        {
            var jpegRenderer = new JpegRenderer(myEffect);
            IBuffer myOutput = await jpegRenderer.RenderAsync();

            MediaLibrary library = new MediaLibrary();
            string fileName = string.Format("Image_{0:G}", DateTime.Now);
            var saveImage = library.SavePicture(fileName, myOutput.AsStream());

            MessageBox.Show("Save la na ja");
        }

        // Sample code for building a localized ApplicationBar
        //private void BuildLocalizedApplicationBar()
        //{
        //    // Set the page's ApplicationBar to a new instance of ApplicationBar.
        //    ApplicationBar = new ApplicationBar();

        //    // Create a new button and set the text value to the localized string from AppResources.
        //    ApplicationBarIconButton appBarButton = new ApplicationBarIconButton(new Uri("/Assets/AppBar/appbar.add.rest.png", UriKind.Relative));
        //    appBarButton.Text = AppResources.AppBarButtonText;
        //    ApplicationBar.Buttons.Add(appBarButton);

        //    // Create a new menu item with the localized string from AppResources.
        //    ApplicationBarMenuItem appBarMenuItem = new ApplicationBarMenuItem(AppResources.AppBarMenuItemText);
        //    ApplicationBar.MenuItems.Add(appBarMenuItem);
        //}
    }
}